/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module;

import com.googlecode.ivybeans.facade.IvyLibrary;
import com.googlecode.ivybeans.facade.IvyLibraryException;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import org.openide.ErrorManager;

/**
 *
 * @author lforet
 */
public class IvyLibraryController {

    private IvyLibrary ivyLibrary;
    private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    public final static String[] DEFAULT_CONFS = new String[]{"*"};
    public final static String SETTINGS_CHANGE = "settingsChange";
    public final static String DESCRIPTOR_CHANGE = "descriptorChange";

    public IvyLibraryController(IvyLibrary ivyLibrary) {
        try {
            this.ivyLibrary = ivyLibrary;
            if (ivyLibrary.getDescriptorURL() != null) {
                setDescriptorFile(new File(ivyLibrary.getDescriptorURL().toURI()));
            }
        } catch (URISyntaxException ex) {
            ErrorManager.getDefault().notify(ex);
        }
    }

    public void setDescriptorFile(File file) {
        try {
            File oldFile = (ivyLibrary.getDescriptorURL() != null) 
                    ? new File(ivyLibrary.getDescriptorURL().toURI())
                    : null;
            URL descriptorURL = (file != null) ? file.toURL() : null;
            ivyLibrary.setDescriptorURL(descriptorURL);
            ivyLibrary.resolve(DEFAULT_CONFS);
            pcs.firePropertyChange(DESCRIPTOR_CHANGE, oldFile, file);
        } catch (IvyLibraryException ex) {
            ErrorManager.getDefault().notify(ex);
        } catch (MalformedURLException ex) {
            ErrorManager.getDefault().notify(ex);
        } catch (URISyntaxException ex) {
            ErrorManager.getDefault().notify(ex);
        }
    }

    public File getDescriptorFile() {
        if (ivyLibrary.getDescriptorURL() != null) {
            try {
                return new File(ivyLibrary.getDescriptorURL().toURI());
            } catch (URISyntaxException ex) {
                ErrorManager.getDefault().notify(ex);
            }
        }
        return null;
    }

    public void setSettingsFile(File file) {
        try {
            File oldFile = (ivyLibrary.getSettingsURL() != null 
                    && !ivyLibrary.getSettingsURL().equals(IvyLibrary.getDefaultSettingsURL()) )
                    ? new File(ivyLibrary.getSettingsURL().toURI())
                    : null;
            URL settingsURL = (file != null) ? file.toURL() : null;
            ivyLibrary.setSettingsURL(settingsURL);
            ivyLibrary.resolve(DEFAULT_CONFS);
            pcs.firePropertyChange(SETTINGS_CHANGE, oldFile, file);
        } catch (IvyLibraryException ex) {
            ErrorManager.getDefault().notify(ex);
        } catch (MalformedURLException ex) {
            ErrorManager.getDefault().notify(ex);
        } catch (URISyntaxException ex) {
            ErrorManager.getDefault().notify(ex);
    }
    }

    public File getSettingsFile() {
        if (ivyLibrary.getDescriptorURL() != null) {
            try {
                return "jar".equalsIgnoreCase(ivyLibrary.getSettingsURL().getProtocol()) 
                        ? null 
                        : new File(ivyLibrary.getSettingsURL().toURI());
            } catch (URISyntaxException ex) {
                ErrorManager.getDefault().notify(ex);
            }
        }
        return null;
    }
    
    public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
        pcs.addPropertyChangeListener(propertyChangeListener);
    }

    public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
        pcs.removePropertyChangeListener(propertyChangeListener);
    }
}
